package com.itheima.mp.domain.query;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

@Data
public class PageQuery {

    private Long pageNo = 1L;

    private Long pageSize = 5L;

    private String sortBy;

    private Boolean isAsc = true;

    public <T> Page<T> toMpPage(OrderItem ... orderItems) {
        Page<T> page = Page.of(pageNo, pageSize);
        if (StrUtil.isNotBlank(sortBy)) {
            page.addOrder(new OrderItem(sortBy, isAsc));
        } else if (ArrayUtil.isNotEmpty(orderItems)){
            page.addOrder(orderItems);
        }
        return page;
    }
}
